package com.alibaba.citrus.ots.application.report.ability.repository.impl;

import com.alibaba.citrus.ots.application.report.ability.repository.MonthSalesCategoryReportQueryRepository;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesBasicReportSDO;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesCategoryReportLoadListRequest;
import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesCategoryReportLoadListVO;
import com.epoch.app.otsapplicationreportsdk.model.dto.QueryMonthSalesReportRequest;
import com.epoch.app.otsapplicationreportsdk.monthsalescategoryreport.model.MonthSalesCategoryReport;
import com.epoch.app.otsapplicationreportsdk.monthsalescategoryreport.service.MonthSalesCategoryReportService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2021/12/10 23:08
 */
@Component
public class MonthSalesCategoryReportQueryRepositoryImpl implements MonthSalesCategoryReportQueryRepository {

    @Resource
    MonthSalesCategoryReportService monthSalesCategoryReportService;

    @Override
    public PageResult<List<MonthSalesBasicReportSDO>> pageQueryList(QueryMonthSalesReportRequest request) {
        MonthSalesCategoryReportLoadListVO monthSalesCategoryReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesCategoryReport>> list = monthSalesCategoryReportService.loadListForPage(monthSalesCategoryReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = list.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,list.getTotal());
    }

    @Override
    public PageResult<List<MonthSalesBasicReportSDO>> pageQueryMonthSalesCategoryReportSortList(QueryMonthSalesReportRequest request) {
        MonthSalesCategoryReportLoadListVO monthSalesCategoryReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesCategoryReport>> list = monthSalesCategoryReportService.loadListForPageSort(monthSalesCategoryReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = list.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,list.getTotal());
    }

    @Override
    public PageResult<List<MonthSalesBasicReportSDO>> pageQueryMonthSalesCompanyCategoryReportSortList(QueryMonthSalesReportRequest request) {
        MonthSalesCategoryReportLoadListVO monthSalesCategoryReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesCategoryReport>> list = monthSalesCategoryReportService.loadCompanyCategoryListForPageSort(monthSalesCategoryReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = list.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return PageResult.of(result,list.getTotal());
    }

    @Override
    public List<MonthSalesBasicReportSDO> getCategoryReportList(QueryMonthSalesReportRequest request) {
        MonthSalesCategoryReportLoadListRequest queryRequest = getListRequestConverter(request);
        Result<List<MonthSalesCategoryReport>> list = monthSalesCategoryReportService.loadList(queryRequest);
        List<MonthSalesBasicReportSDO> result = list.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return result;
    }

    @Override
    public List<MonthSalesBasicReportSDO> statisticalQuery(QueryMonthSalesReportRequest request) {
        MonthSalesCategoryReportLoadListVO monthSalesCategoryReportLoadListVO = loadListRequestConverter(request);
        Result<List<MonthSalesCategoryReport>> response = monthSalesCategoryReportService.statisticalQuery(monthSalesCategoryReportLoadListVO);
        List<MonthSalesBasicReportSDO> result = response.getResult().stream().map(x -> {
            MonthSalesBasicReportSDO bean = new MonthSalesBasicReportSDO();
            BeanUtils.copyProperties(x,bean);
            return bean;
        }).collect(Collectors.toList());
        return result;
    }

    private MonthSalesCategoryReportLoadListVO loadListRequestConverter(QueryMonthSalesReportRequest request){
        MonthSalesCategoryReportLoadListVO listVO = MonthSalesCategoryReportLoadListVO.newMonthSalesCategoryReportLoadListVOBuilder()
                .orgBusinessUnitIds(request.getOrgBusinessUnitIds())
                .orgSalesOrganizationIds(request.getOrgSalesOrganizationIds())
                .orgSalesChannelIds(request.getOrgSalesChannelIds())
                .orgSalesDepartmentIds(request.getOrgSalesDepartmentIds())
                .scItemCategoryIds(request.getScItemCategoryIds())
                .scItemCategoryName(request.getScItemCategoryName())
                .bizType(request.getBizType())
                .statisticalDate(request.getStatisticalDate())
                .dataMark(request.getDataMark())
                .groupBy(request.getGroupBy())
                .build();
        listVO.setStart(request.getStart());
        listVO.setLimit(request.getLimit());
        return listVO;
    }

    private MonthSalesCategoryReportLoadListRequest getListRequestConverter(QueryMonthSalesReportRequest request){
        MonthSalesCategoryReportLoadListRequest listRequest = MonthSalesCategoryReportLoadListRequest.newMonthSalesCategoryReportLoadListRequestBuilder()
                .orgBusinessUnitId(request.getOrgBusinessUnitId())
                .orgSalesOrganizationId(request.getOrgSalesOrganizationId())
                .orgSalesChannelId(request.getOrgSalesChannelId())
                .orgSalesDepartmentId(request.getOrgSalesDepartmentId())
                .bizType(request.getBizType())
                .scItemCategoryId(request.getScItemCategoryId())
                .planTime(request.getPlanMonth())
                .statisticalDate(request.getStatisticalDate())
                .build();
        return listRequest;
    }

}
